package com.cctc.juc.common.thread.threadpool.holder;

import com.cctc.juc.common.thread.ShutdownHookThread;
import com.cctc.juc.common.util.ThreadUtils;

import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * 调度 “串行化定时” 型任务线程池。
 * - 懒汉式创建单例调度 “串行化定时” 型任务线程池，用于定时执行 FIFO “串行化” 任务。
 */
public class SeqOrScheduledTargetThreadPoolLazyHolder {
    private static final ScheduledThreadPoolExecutor EXECUTOR = new ScheduledThreadPoolExecutor(
            1,
            new ThreadUtils.AppThreadFactory("seq")
    );

    public static ScheduledThreadPoolExecutor getExecutor() {
        return EXECUTOR;
    }

    static {
        // JVM 进程关闭时的钩子函数
        Runtime.getRuntime().addShutdownHook(new ShutdownHookThread("“串行化定时” 型任务线程池", () -> {
            // 优雅终止线程池
            ThreadUtils.shutdownThreadPoolGracefully(EXECUTOR);
            return null;
        }));
    }


}
